package com.tcfvic.client.panels.tools;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.SimpleMessagePopup;

public class UploadKirsteeSpreadsheet {

	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);

	public UploadKirsteeSpreadsheet() {

	}

	public Widget getContent() {
		VerticalPanel contents = new VerticalPanel();
		contents.setWidth("500");
//		HTML link = getEmptyDatabaseLink(contents);
//		if (link != null) {
//			contents.add(link);
//		}

//		link = new HTML("Upload Excel spreadsheet (Format: Kirstee)");
//		contents.add(link);
//		link.addMouseDownHandler(new MouseDownHandler() {
//
//			@Override
//			public void onMouseDown(MouseDownEvent event) {
//				final DialogBox dialogBox = getFileUploadContents("tcfvic/uploadKirsteeFile");
//				dialogBox.setAnimationEnabled(true);
//				dialogBox.center();
//				dialogBox.show();
//			}
//		});
//
//		link = new HTML("Upload Excel spreadsheet (Format: Houston)");
//		contents.add(link);
//		link.addMouseDownHandler(new MouseDownHandler() {
//
//			@Override
//			public void onMouseDown(MouseDownEvent event) {
//				final DialogBox dialogBox = getFileUploadContents("tcfvic/uploadHoustonFile");
//				dialogBox.setAnimationEnabled(true);
//				dialogBox.center();
//				dialogBox.show();
//			}
//		});

		HTML link = new HTML("Upload Excel spreadsheet (Format: All Data)");
		contents.add(link);
		link.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {
				final DialogBox dialogBox = getFileUploadContents("tcfvic/empAllDataFile");
				dialogBox.setAnimationEnabled(true);
				dialogBox.center();
				dialogBox.show();
			}
		});

		link = new HTML("Upload Employment Status");
		contents.add(link);
		link.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {
				final DialogBox dialogBox = getFileUploadContents("tcfvic/empStatusFile");
				dialogBox.setAnimationEnabled(true);
				dialogBox.center();
				dialogBox.show();
			}
		});

		link = new HTML("Upload JSAP file");
		contents.add(link);
		link.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {
				final DialogBox dialogBox = getFileUploadContents("tcfvic/jsapFile");
				dialogBox.setAnimationEnabled(true);
				dialogBox.center();
				dialogBox.show();
			}
		});

		link = new HTML("Upload RTO file");
		contents.add(link);
		link.addMouseDownHandler(new MouseDownHandler() {
			
			@Override
			public void onMouseDown(MouseDownEvent event) {
				final DialogBox dialogBox = getFileUploadContents("tcfvic/rtoFile");
				dialogBox.setAnimationEnabled(true);
				dialogBox.center();
				dialogBox.show();
			}
		});
		
		return contents;
	}

	private HTML getEmptyDatabaseLink(VerticalPanel contents) {
		final HTML link = new HTML();
		personService.isAdminUser(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					link.setText("Empty database");
					link.addMouseDownHandler(new MouseDownHandler() {
						@Override
						public void onMouseDown(MouseDownEvent event) {
							personService
									.truncateDatabase(new AsyncCallback<Void>() {

										@Override
										public void onSuccess(Void result) {
											new SimpleMessagePopup(
													"Database emptied", null)
													.showPopup();
										}

										@Override
										public void onFailure(Throwable caught) {
											new SimpleMessagePopup(
													"Error in query: " + caught,
													null).showPopup();
										}
									});
						}
					});
				}
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		});

		return link;
	}

	private DialogBox getFileUploadContents(String action) {
		final DialogBox dialogBox = new DialogBox();

		// Create a FormPanel and point it at a service.
		final FormPanel form = new FormPanel();
		form.setAction(action);
		// form.setAction("/TCF/tcfvic/uploadKirsteeFile");

		// Because we're going to add a FileUpload widget, we'll need to set the
		// form to use the POST method, and multipart MIME encoding.
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);

		// Create a vertical panel to align the content
		VerticalPanel vPanel = new VerticalPanel();

		// Add a label
		vPanel.add(new HTML("Select a file"));

		// Add a file upload widget
		final FileUpload fileUpload = new FileUpload();
		fileUpload.setName("uploadFormElement");
		vPanel.add(fileUpload);

		// Add a button to upload the file
		HorizontalPanel hp = new HorizontalPanel();
		Button uploadButton = new Button("Upload file");
		uploadButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String filename = fileUpload.getFilename();
				if (filename.length() == 0) {
					Window
							.alert("Please make sure you select a valid Excel file (xls)");
				} else {
					form.submit();
					dialogBox.hide();
				}
			}
		});
		Button closeButton = new Button("Cancel", new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		hp.add(uploadButton);
		hp.add(closeButton);

		vPanel.add(new HTML("<br>"));
		vPanel.add(hp);

		form.setWidget(vPanel);

		// Add an event handler to the form.
		form.addSubmitHandler(new SubmitHandler() {

			@Override
			public void onSubmit(SubmitEvent event) {
				// This event is fired just before the form is submitted. We can
				// take
				// this opportunity to perform validation.
				if (fileUpload.getFilename().isEmpty()) {
					Window.alert("Plesae select a file");
				}
			}
		});

		form.addSubmitCompleteHandler(new SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				Window.alert(event.getResults());
			}
		});

		// Return the layout panel
		dialogBox.setWidget(form);
		return dialogBox;

	}
}
